<?php namespace Phpcmf\Controllers;

class Duixin extends \Phpcmf\Home\Module
{

	public function addtext() {
		//发布文字短信
		$datatext = \Phpcmf\Service::L('input')->post('data');
		if (IS_POST) {
		    
			if (!$this->uid) {
			    
			    $this->_json(0, dr_lang('还没有登录'));
			} elseif (!$datatext['content']) {
			    
			    $this->_json(0, dr_lang('内容不能为空'));
			} elseif (!$datatext['shouxinren']) {
			    
			    $this->_json(0, dr_lang('收信人不能为空'));
			}
			$data = [
			 'catid'=> 1, //私信
			 'title' => '私信', //标题
			 'zhiding' => '', //置顶
			 'content'=> $datatext['content'], //通知内容
			 'uid'=> $this->uid, //发布人的uid
			 'faxinren'=> $this->uid, //发布人的uid
			 'shouxinren'=> $datatext['shouxinren'],//收信人
			 'tupian'=> '', //图片
			 'shipin'=> '', //视频
			 'yuyin'=> '', //语音
			 'fujian'=> '', //附件
			 'yidu'=> 0, //已读
			 'quanziid'=> '', //圈子ID
			 'quantieid'=> '', //圈帖ID
			 'url' => '',
			];
			\Phpcmf\Service::M('auth')->addtongzhi($data);
			$msg = "提交成功";
			
		}
		$this->_json(1, $msg, $datatext);
	}
	//已读
	public function yidu() {
		$touid = \Phpcmf\Service::L('input')->get('touid');
		if (!$this->uid) {
		    $this->_json(0, dr_lang('还没有登录'));
		} elseif (!$touid) {
		    
		    $this->_json(0, dr_lang('touid不能为空'));
		}
		$table = '1_duixin';
		\Phpcmf\Service::M()->db->table($table)->where('uid', $this->uid)->where('shouxinren', $touid)->update(['yidu' => 1]);
	    $msg = "处理已读成功";
		$this->_json(1, $msg, 1);
	}
	// 列表
	public function list() {
		if (!$this->uid) {
		    $this->_json(0, dr_lang('还没有登录'));
		}
		$table = '1_duixin';
		$data = \Phpcmf\Service::M()->db->table($table)->where('catid', 1)->where('uid', $this->uid)->get()->getResultArray();
	    $msg = "读取成功";
		$this->_json(1, $msg, $data);
	}
	//判断好友
	public function ishaoyou(){
	    $touid = \Phpcmf\Service::L('input')->get('touid');
	    $rt = \Phpcmf\Service::M()->db->table('1_duixin_form_hylb')->where('uid', $this->uid)->where('touid', $touid)->countAllResults();
		$rttouid = \Phpcmf\Service::M()->db->table('1_duixin_form_hylb')->where('uid', $touid)->where('touid', $this->uid)->countAllResults();
	    
	    if(!$this->uid){
	        $datas['zhuangtai'] = 10;
	        $msg ='待登录';
	    } 
	    if (!$rt && !$rttouid) {
	        $datas['zhuangtai'] = 11;
	        $msg ='待申请好友';
	    } 
	    
	    $data = \Phpcmf\Service::M()->db->table('1_duixin_form_hylb')->where('uid', $this->uid)->where('touid', $touid)->get()->getRowArray();
	    $datatouid = \Phpcmf\Service::M()->db->table('1_duixin_form_hylb')->where('uid', $touid)->where('touid', $this->uid)->get()->getRowArray();
	    if($data){
	        if ($data['zhuangtai']==1) {
    	        $datas['zhuangtai'] = 1;
    	        $msg ='好友';
    	    } elseif ($data['zhuangtai']==0) {
    	        $datas['zhuangtai'] = 0;
    	        $msg ='待核对';
    	    } elseif ($data['zhuangtai']==2) {
    	        $datas['zhuangtai'] = 2;
    	        $msg ='黑名单';
    	    } else {
    	        $datas['zhuangtai'] = 5;
    	        $msg ='待加好友';
    	    }
	    } else {
	        
	        if ($datatouid['zhuangtai']==1) {
    	        $datas['zhuangtai'] = 1;
    	        $msg ='好友';
    	    } elseif ($datatouid['zhuangtai']==0) {
    	        $datas['zhuangtai'] = 0;
    	        $msg ='待核对';
    	    } elseif ($datatouid['zhuangtai']==2) {
    	        $datas['zhuangtai'] = 2;
    	        $msg ='黑名单';
    	    } else {
    	        $datas['zhuangtai'] = 6;
    	        $msg ='待加好友';
    	    }
	    }
	    $this->_json(1, $msg, $datas);
	}
	//互加
	public function hujia(){
	    if (!$this->uid) {
		    $this->_json(0, dr_lang('还没有登录'));
		}
		$datahujia = \Phpcmf\Service::L('input')->post('data');
		if (!$datahujia['touid']) {
		    $this->_json(0, dr_lang('不能为空'));
		}
		$nutouid = dr_member_info($datahujia['touid']);
		if(!$nutouid){
		    $this->_json(0, dr_lang('没有这个用户'));
		}
		$datas = [
            //'site' => (int)$site,
            'cid'=>1,
            'catid'=> 1, //类型
            'uid' => $this->uid, //用户
            'title'=> '加好友',
            'touid'=> $datahujia['touid'],
            'inputip'=>'',
            'status' =>1,
            'zhuangtai' => 0, //待添加 1好友。2黑名单
            'updatetime' =>SYS_TIME,
            'inputtime' => SYS_TIME,
        ];
        $datac = [
            'cid'=> 1,
            'uid'=> $this->uid, //发布人的uid
            'catid'=> 1, //类型
        ];
        $dataduixin = [
			 'catid'=> 7, //私信
			 'title' => '加友', //标题
			 'zhiding' => '', //置顶
			 'content'=> '好友添加', //通知内容
			 'uid'=> $this->uid, //发布人的uid
			 'faxinren'=> $this->uid, //发布人的uid
			 'shouxinren'=> $datahujia['touid'],//收信人
			 'tupian'=> '', //图片
			 'shipin'=> '', //视频
			 'yuyin'=> '', //语音
			 'fujian'=> '', //附件
			 'yidu'=> 0, //已读
			 'quanziid'=> '', //圈子ID
			 'quantieid'=> '', //圈帖ID
			 'url' => '',
			];
			
		if (IS_POST) {
		    \Phpcmf\Service::M()->db->table('1_duixin_form_hylb')->insert($datas);
            \Phpcmf\Service::M()->db->table('1_duixin_form_hylb_data_0')->insert($datac);
            //提示
            \Phpcmf\Service::M('auth')->addtongzhi($dataduixin);
            $msg = "提交成功";
		}
		
		$this->_json(1, $msg, 1);
	}
	//认证添加好友
	public function renzhenghujia(){
	    $datarenzheng = \Phpcmf\Service::L('input')->post('data');
	    if (IS_POST) {
	    \Phpcmf\Service::M()->db->table('1_duixin_form_hylb')->where('uid', $datarenzheng['touid'])->where('touid', $this->uid)->update(['zhuangtai' => 1]);
	    if($datarenzheng['zhuangtai'] == 1){
	        $dataduixin = [
			 'catid'=> 1, //私信
			 'title' => '私信', //标题
			 'zhiding' => '', //置顶
			 'content'=> '您好!', //通知内容
			 'uid'=> $this->uid, //发布人的uid
			 'faxinren'=> $this->uid, //发布人的uid
			 'shouxinren'=> $datarenzheng['touid'],//收信人
			 'tupian'=> '', //图片
			 'shipin'=> '', //视频
			 'yuyin'=> '', //语音
			 'fujian'=> '', //附件
			 'yidu'=> 0, //已读
			 'quanziid'=> '', //圈子ID
			 'quantieid'=> '', //圈帖ID
			 'url' => '',
			];
	        $msg = "认证成功成功";
	        
	        \Phpcmf\Service::M('auth')->addtongzhi($dataduixin);
	    }
	    } elseif ($datarenzheng['zhuangtai'] == 2) {
	        $msg = "拒绝添加";
	    }
		$this->_json(1, $msg, 1);
	}

}
